
import * as actions from './';
//   import BSPError from './../common/bsperror';
import HttpRequest from './../common/http';
import { showError } from './a_personal_task';
  
const rootUrl = window.restConfig.rootUrl;

export const scheduleInit = () => {
  return (dispatch, getState) => {
    dispatch({
    type: actions.SCHEDULE_INIT
    })
  }
}
  
export const getSchedules = (periodId) => {
  return (dispatch, getStore) => {
    HttpRequest.get(rootUrl + '/scheduler/list/'+periodId)
      .then((ret) => {
        dispatch({
          type: actions.SCHEDULE_CHANGE,
          data: {
            state: ret.data || {}
          }
        })
      }).catch((err) => {
        dispatch(showError('加载数据失败'));
      });
  }
}

export const moreSchedules = (periodId) => {
  return (dispatch, getStore) => {
    let state = getStore().schedule || {};
    HttpRequest.get(rootUrl + '/scheduler/more/list/'+periodId)
    .then((ret) => {
        let newData = state.state.data;
        let keys = (ret.data&&ret.data.data&&Object.keys(ret.data.data)) || [];
        keys.forEach( (key) => {
          if(newData[key]){
            newData[key] = newData[key].concat(ret.data.data[key]);
          }else{
            newData[key] = ret.data.data[key];
          }
        })
        dispatch({
          type: actions.SCHEDULE_CHANGE,
          data: {
            state: {
              data: newData,
              page: ret.data.page
            }
          }
        })
      }).catch((err) => {
        dispatch(showError('加载数据失败'));
      });
  }
}